package com.controller;

import com.entity.WeiJianDispatchRecord;
import com.service.WeiJianDispatchRecordService;
import com.dto.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Optional;

@RestController
@RequestMapping("/api/weijian/worker")
public class WeiJianDispatchRecordController {

    @Autowired
    private WeiJianDispatchRecordService weiJianDispatchRecordService;

    @PostMapping
    public WeiJianDispatchRecord create(@RequestBody WeiJianDispatchRecord weiJianDispatchRecord) {
        return weiJianDispatchRecordService.save(weiJianDispatchRecord);
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable(value = "id") Integer id) {
        weiJianDispatchRecordService.deleteById(id);
    }

    @PutMapping("/{id}")
    public WeiJianDispatchRecord update(@PathVariable(value = "id") Integer id, @RequestBody WeiJianDispatchRecord weiJianDispatchRecord) {
        weiJianDispatchRecord.setId(id);
        return weiJianDispatchRecordService.save(weiJianDispatchRecord);
    }

    @GetMapping("/{id}")
    public WeiJianDispatchRecord detail(@PathVariable(value = "id") Integer id) {
        return weiJianDispatchRecordService.findById(id).orElse(null);

    }

    @GetMapping("/page")
    public Page<WeiJianDispatchRecord> page(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "10") int pageSize,
                       @RequestParam(required = false) String projectName) {
        Page<WeiJianDispatchRecord> page;
        if (projectName != null && !projectName.isEmpty()) {
            page = weiJianDispatchRecordService.findByProjectNameContaining(projectName, pageNum, pageSize);
        } else {
            page = weiJianDispatchRecordService.findAll(pageNum, pageSize);
        }
        return page;
    }
}